﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

using Microsoft.Xna.Framework.Input;

using SmlEngine.UI.Managers;

namespace SmlEngine.UI.Input
{
    public class StringReader
    {
        private InputReader keys;

        private StringBuilder builder;

        public int CaratPosition { get; set; }

        public string Text { get { return builder.ToString(); } }

        public Action EnterPressed { get; set; }

        public StringReader()
        {
            keys = new InputReader();
            builder = new StringBuilder();
        }

        public void Append(string value)
        {
            builder.Append(value);
        }

        public void Update(InputManager input)
        {
            keys.Update(input);

            for (int i = 0; i < keys.Input.Count; i++)
            {
                var key = keys.Input[i];

                bool control = key == Keys.LeftControl || key == Keys.RightControl;
                bool shift = key == Keys.LeftShift || key == Keys.RightShift;
                bool alt = key == Keys.LeftAlt || key == Keys.RightAlt;

                keys.Input.RemoveAt(i);

                if (key == Keys.Left)
                    CaratPosition = Math.Max(CaratPosition - 1, 0);
                else if (key == Keys.Right)
                    CaratPosition = Math.Min(CaratPosition + 1, builder.Length);
                else if (key == Keys.Back)
                {
                    if (CaratPosition == 0)
                        continue;
                         
                    builder.Remove(CaratPosition - 1, 1);
                    CaratPosition--;
                }
                else if (key == Keys.Delete)
                {
                    if (CaratPosition == builder.Length)
                        continue;

                    builder.Remove(CaratPosition, 1);
                }
                else if (key == Keys.Enter)
                {
                    EnterPressed();
                }
                else if (!shift && !control && !alt)
                {
                    if (!keys.IsShiftDown && (int)key >= 65 && (int)key <= 97)
                        key += 32;

                    builder.Insert(CaratPosition, (char)key);
                    CaratPosition++;
                }
            }
        }
    }

    public class InputReader
    {
        private List<Keys> keys;

        public IList<Keys> Input { get { return keys; } }

        public bool IsShiftDown { get; private set; }

        public InputReader()
        {
            keys = new List<Keys>();
        }

        public void Update(InputManager input)
        {
            var pressedKeys = input.Current.KeyboardState.GetPressedKeys();

            bool isShiftDown = false;

            for (int i = 0; i < pressedKeys.Length; i++)
            {
                if (input.Previous.KeyboardState.IsKeyUp(pressedKeys[i]))
                {
                    keys.Add(pressedKeys[i]);
                }

                if (pressedKeys[i] == Keys.LeftShift ||
                    pressedKeys[i] == Keys.RightShift)
                    isShiftDown = true;
            }

            IsShiftDown = isShiftDown;
        }
    }
}
